package mipushdemo.xiaomi.com.diyitonghang.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract  class BaseActivity extends AppCompatActivity {
    private BaseFragment baseFragment;
    private Unbinder bind;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow(windowColor());
        setContentView(getLayoutId());
        bind = ButterKnife.bind(this);
        init();
        loadData();

    }
    protected  void initWindow(int color) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//因为不是所有的系统都可以设置颜色的，在4.4以下就不可以。。有的说4.1，所以在设置的时候要检查一下系统版本是否是4.1以上
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(color));

        }
    }
    public void changeStatusBarTextColor(boolean isBlack) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            if (isBlack) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            }else {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
            }
        }
    }

    protected  abstract int windowColor();

    protected abstract void init();

    protected abstract void loadData();

    protected abstract int getLayoutId();

    public void setCreateView(int container, Class<? extends BaseFragment> fragmentClass, Bundle bundle) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        String simpleName = fragmentClass.getSimpleName();
        Fragment fragment = manager.findFragmentByTag(simpleName);
        if (fragment == null) {
            try {
                fragment = fragmentClass.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            transaction.add(container, fragment, simpleName);
        }

        if (bundle != null) {
            fragment.setArguments(bundle);
        }

        if (baseFragment != null) {
            transaction.hide(baseFragment);
        }
        transaction.show(fragment);
        baseFragment = (BaseFragment) fragment;
        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
